/**
 * Created by Administrator on 2018/11/14.
 */
var baseUrl = 'http://114.116.128.243:8080/';

//post请求
function ajaxPost(obj) {
	var url;
	var data;
	if(!obj.url) {
		console.log("缺少请求地址")
		return
	}
	if(!obj.data) {
		obj.data = ''
	}
	url = baseUrl + obj.url;
	data = obj.data;
	$.ajax({
		url: url,
		type: "POST",
		//headers: {
		//    'Access-Token':$.cookie('access_token')
		//},
		data: data,
		//contentType:"application/json",
		traditional: true,
		dataType: 'json',
		timeout: 30000,
		success: function(res) {
			if(res.code == 200 || res.status == 'success') {
				obj.successFull(res)
			} else {
				obj.errCallBack(res)
			}

		},
		error: function(err) {
			layer.msg('加载失败了，稍后重试一下吧', {
				icon: 5
			});
		},
	})
}
//get请求
function ajaxGet(obj) {
	var url;
	var data;
	if(!obj.url) {
		console.log("缺少请求地址")
		return
	};
	if(!obj.data) {
		obj.data = ''
	};
	if(!obj.async) {
		obj.async = true;
	} else {
		obj.async = false;
	}
	url = baseUrl + obj.url;
	data = obj.data;
	$.ajax({
		url: url,
		type: "GET",
		async: obj.async,
		//headers: {
		//    'Access-Token':$.cookie('access_token')
		//},
		data: data,
		dataType: 'json',
		timeout: 30000,
		success: function(res) {
			if(res.code == 200) {
				obj.successFull(res)
			} else {
				obj.errCallBack(res)
			}
		},
		error: function(err) {
			layer.msg('加载失败了，稍后重试一下吧', {
				icon: 5
			});
		},
	})
}

//table表格请求
function ajaxTable(obj) {
	var url;
	var dataCols;
	var height;
	var statusName;
	var statusCode;
	var msgName;
	var countName;
	var dataName;
	var urlParme;
	var toolbar;
	if(!obj.url) {
		console.log("缺少请求地址");
		return;
	}
	if(!obj.dataCols) {
		console.log("缺少表格数据表头");
		return;
	};
	if(!obj.toolbar) {
		obj.toolbar = '#toolbarDemo';
	};
	if(!obj.height) {
		obj.height = "auto";
	};
	if(!obj.statusName) {
		obj.statusName = "code";
	};
	if(!obj.statusCode) {
		obj.statusCode = "200";
	};
	if(!obj.msgName) {
		obj.msgName = 'msg';
	};
	if(!obj.countName) {
		obj.countName = "total";
	};
	if(!obj.dataName) {
		obj.dataName = "list";
	};
	if(!obj.urlParme) {
		obj.urlParme = "";
	};
	url = baseUrl + obj.url;
	data = obj.data;
	height = obj.height;
	dataCols = obj.dataCols;
	statusName = obj.statusName;
	statusCode = obj.statusCode;
	msgName = obj.msgName;
	countName = obj.countName;
	dataName = obj.dataName;
	urlParme = obj.urlParme;
	toolbar = obj.toolbar;
	console.log(urlParme);

	layui.use(['table', 'laypage'], function() {
		var table = layui.table;
		var laypage = layui.laypage;
		table.render({
			elem: obj.idNode,
			height: height,
			toolbar: toolbar,
			//defaultToolbar:['layui-icon-cols', 'layui-icon-export', 'layui-icon-print'],
			url: url + '?pageNo=1' + urlParme, //规定请求地址
			cols: [dataCols],
			response: {
				statusName: statusName, //规定数据状态的字段名称，默认：code
				statusCode: statusCode, //规定成功的状态码，默认：200
				msgName: msgName, //规定状态信息的字段名称，默认：msg
				countName: countName, //规定数据总数的字段名称，默认：count
				dataName: dataName //规定数据列表的字段名称，默认：data
			},
			done: function(res, curr, count) {
				if(obj.successFull) {
					obj.successFull(res, curr, count, table)
				}
				if(!obj.page) {
					laypage.render({
						elem: 'layui-table-page1',
						layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
						limit: 15,
						limits: [15],
						count: count,
						last: true,
						jump: function(pageCurr, first) {
							if(!first) {
								table.render({
									elem: obj.idNode,
									height: height,
									url: url + '?pageNo=' + pageCurr.curr + urlParme, //规定请求地址
									cols: [dataCols],
									toolbar: toolbar,
									//defaultToolbar:['layui-icon layui-icon-cols', 'layui-icon layui-icon-export', 'layui-icon layui-icon-print'],
									response: {
										statusName: statusName, //规定数据状态的字段名称，默认：code
										statusCode: statusCode, //规定成功的状态码，默认：0
										msgName: msgName, //规定状态信息的字段名称，默认：msg
										countName: countName, //规定数据总数的字段名称，默认：count
										dataName: dataName //规定数据列表的字段名称，默认：data
									},
									done: function(res, curr, count) {
										if(obj.successFull) {
											obj.successFull(res, curr, count, table)
										}
									}
								});
							}
						}
					});
				}
			}
		});

	});
};
//处理时间格式
function crtTimeFtt() {
//  if (val != null) {
            var date = new Date();
            return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
//      }
}
//function ajaxTable(obj) {
//	var url;
//	var dataCols;
//	var height;
//	var statusName;
//	var statusCode;
//	var msgName;
//	var countName;
//	var dataName;
//	if(!obj.url) {
//		console.log("缺少请求地址")
//		return
//	}
//	if(!obj.dataCols) {
//		console.log("缺少表格数据表头");
//		return
//	}
//	if(!obj.height) {
//		obj.height = "auto"
//	};
//	if(!obj.statusName) {
//		obj.statusName = "code"
//	}
//	if(!obj.statusCode) {
//		obj.statusCode = "200"
//	}
//	if(!obj.msgName) {
//		obj.msgName = 'msg'
//	}
//	if(!obj.countName) {
//		obj.countName = "total"
//	}
//	if(!obj.dataName) {
//		obj.dataName = "list"
//	}
//	url = baseUrl + obj.url;
//	data = obj.data;
//	height = obj.height;
//	dataCols = obj.dataCols;
//	statusName = obj.statusName;
//	statusCode = obj.statusCode;
//	msgName = obj.msgName;
//	countName = obj.countName;
//	dataName = obj.dataName;
//	layui.use(['table,laypage'], function() {
//		var table = layui.table;
//		var laypage = layui.laypage;
//		//实例化表格
//		table.render({
//			elem: obj.idNode,
//			height: height,
//			url: url, //规定请求地址
//			cols: [dataCols],
//			response: {
//				statusName: statusName, //规定数据状态的字段名称，默认：code
//				statusCode: statusCode, //规定成功的状态码，默认：0
//				msgName: msgName, //规定状态信息的字段名称，默认：msg
//				countName: countName, //规定数据总数的字段名称，默认：count
//				dataName: dataName //规定数据列表的字段名称，默认：data
//			},
//			done: function(res, curr, count) {
//				if(obj.successFull) {
//					obj.successFull(res, curr, count)
//				}
//			}
//		});
//	});
//}